package com.ax.base.system.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ax.base.common.domain.BaseArea;
import com.ax.base.common.domain.Tree;
import com.ax.base.system.domain.MenuDO;


public interface MenuMapper {
	@Select("SELECT  menu_id, parent_id, name, url, perms, `type`, icon, order_num, gmt_create, gmt_modified,sys_type FROM sys_menu where menu_id =#{id}")
	MenuDO getMenu(Long id);

	@Select("select distinct m.menu_id , parent_id, name, url, perms, `type`, icon, order_num, gmt_create, gmt_modified,m.sys_type from sys_menu m left join sys_role_menu rm on m.menu_id = rm.menu_id left join sys_user_role ur on rm.role_id = ur.role_id where ur.user_id = #{id} and m.type in(0,1) order by m.type")
	List<MenuDO> listMenuByUserId(Long id);

	@Select("select m.perms from sys_menu m left join sys_role_menu rm on m.menu_id = rm.menu_id left join sys_user_role ur on rm.role_id = ur.role_id where ur.user_id = #{id} ")
	List<String> listUserPerms(Long id);

	@Select("SELECT menu_id, parent_id, name, url, perms, `type`, icon, order_num, gmt_create, gmt_modified,sys_type sysType FROM sys_menu order by type")
	List<MenuDO> listMenuOther();
	
	@Select("SELECT menu_id, parent_id, name, url, perms, `type`, icon, order_num, gmt_create, gmt_modified,sys_type sysType FROM sys_menu where sys_type = 0 order by type")
	List<MenuDO> listMenu();
	
	@Delete("delete from sys_menu where menu_id = #{id}")
	int remove(Long id);

	@Insert("insert sys_menu(parent_id,name,url,perms,type,sys_type) values(#{parentId},#{name},#{url},#{perms},#{type},#{sysType})")
	int save(MenuDO menu);

	@Update("UPDATE sys_menu SET  name=#{name}, url=#{url},  icon=#{icon},type=#{type},perms=#{perms},sys_type=#{sysType} WHERE menu_id=#{menuId}")
	int update(MenuDO menu);
	
	/**
	 * 行政规划查询(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseArea>
	 */
    @Select("<script>" +
    		 "select area_id areaId, area_code, area_name areaName, first_letter, full_name, parent_area_id parentAreaId, area_type,"+ 
             "Registrar_ID, Registrar_Name, create_time, update_time, Flag from base_area" + 
    		 "<where>" + 
    	  	 "<if test=\"areaId != null\">"+ "and area_id = #{areaId} " + "</if>" + 
    	  	 "</where>"+ 
    	  	 "<if test=\"offset != null and limit != null\">"+
			 "limit #{offset}, #{limit}" + 
			 "</if>"+
    		 "</script>")
    List<BaseArea> queryArealistMapper(Map<String,Object> map);
    
    @Select("<script>" +
   		     "select count(0) from base_area"+ 
	   		 "<where>" + 
	   	  	 "<if test=\"areaId != null\">"+ "and area_id = #{areaId} " + "</if>" + 
	   	  	 "</where>"+ 
   		 "</script>")
    Integer count(Map<String,Object> map);
    
    /**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return BaseArea
	 */
    @Select("<script>" +
   		     "select area_id areaId, area_code, area_name areaName, first_letter, full_name, parent_area_id parentAreaId, area_type,"+ 
             "Registrar_ID, Registrar_Name, create_time, update_time, Flag from base_area" + 
	   		 "<where>" + 
	   	  	 "<if test=\"areaId != null\">"+ "and area_id = #{areaId} " + "</if>" + 
	   	  	 "</where>"+ 
   		 "</script>")
    BaseArea getAreaMapper(@Param("areaId") Long areaId);
    
    /**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param BaseArea baseArea
	 *           
	 * @return SUCC
	 */
    @Insert("insert into base_area (area_id, area_code, area_name,"+
			  "first_letter, full_name, parent_area_id,"+ 
			  "area_type, Registrar_ID, Registrar_Name,"+ 
			  "create_time, update_time, Flag"+
			  ")"+
			  "values (#{areaId}, #{areaCode}, #{areaName},"+
			  "#{firstLetter}, #{fullName}, #{parentAreaId},"+
			  "#{areaType}, #{registrarId}, #{registrarName},"+
			  "NOW(), #{updateTime}, #{flag}"+
			  ")")
    void insertAreaMapper(BaseArea baseArea);
    
    @Update("<script>" +
    		"update base_area"+
			   "<set >"+
			   "<if test=\"areaCode != null and areaCode != ''\">"+ "area_code = #{areaCode}, " + "</if>" + 
			   "<if test=\"areaName != null and areaName != ''\">"+ "area_name = #{areaName}, " + "</if>" + 
			   "<if test=\"firstLetter != null and firstLetter != ''\">"+ "first_letter = #{firstLetter}, " + "</if>" + 
			   "<if test=\"fullName != null and fullName != ''\">"+ "full_name = #{fullName}, " + "</if>" + 
			   "<if test=\"parentAreaId != null\">"+ "parent_area_id = #{parentAreaId}, " + "</if>" + 
			   "<if test=\"areaType != null\">"+ "area_type = #{areaType}, " + "</if>" + 
			   "<if test=\"registrarId != null\">"+ "Registrar_ID = #{registrarId}, " + "</if>" + 
			   "<if test=\"registrarName != null and registrarName != ''\">"+ "Registrar_Name = #{registrarName}, " + "</if>" + 
			   "<if test=\"createTime != null\">"+ "create_time = #{createTime}, " + "</if>" + 
			   "update_time = NOW()," + 
			   "<if test=\"flag != null\">"+ "Flag = #{flag}, " + "</if>" + 
			   "</set>"+
			   "where area_id = #{areaId,jdbcType=BIGINT}"+ 
			   "</script>")
    void updateAreaMapper(BaseArea baseArea);
    
    @Select("SELECT area_id id,area_name text, parent_area_id parentId FROM base_area")
    List<Tree<MenuDO>> listPareaId();
    
    
}
